package com.example.ceramics.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.ceramics.R;
import com.example.ceramics.adapter.GoodsAdapter;
import com.example.ceramics.database.dao.GoodsDaoImpl;
import com.example.ceramics.database.dao.IGoodsDao;
import com.example.ceramics.entity.GoodsEntity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

public class SearchActivity extends BaseActivity {
    private TextView tvTitle;
    private ImageView ivReturn;

    private RecyclerView recyclerView;
    private HashMap searchMap;
    private GoodsAdapter goodsAdapter;
    private List<GoodsEntity> list;
    private String searchWord;

    private TextView tvFailure;

    private IGoodsDao goodsDao;
//    private String getSearchWord;

    @Override
    protected int initLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        tvTitle = findViewById(R.id.tv_top_title);
        ivReturn = findViewById(R.id.iv_return);
        recyclerView = findViewById(R.id.recyclerView);

        tvFailure = findViewById(R.id.tv_failure);
    }

    @Override
    protected void initData() {
        goodsDao = new GoodsDaoImpl(mContext);
//        searchMap = (HashMap) getIntent().getSerializableExtra("searchMap");
//        list = (List<GoodsEntity>) searchMap.get("list");
//        searchWord = (String) searchMap.get("searchWord");
        searchWord = (String) getIntent().getSerializableExtra("searchWord");
        list = goodsDao.searchGoods(searchWord);

        tvTitle.setText(searchWord);

        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if(list.isEmpty()){
            recyclerView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0));
            tvFailure.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));
            showToast("没有找到相关物品！");
            return;
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(gridLayoutManager);

        goodsAdapter = new GoodsAdapter(this,list);
        recyclerView.setAdapter(goodsAdapter);
        goodsAdapter.setOnItemClickListener(new GoodsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Serializable obj) {
                GoodsEntity goodsEntity = (GoodsEntity)obj;
                navigateTo(DetailActivity.class, "detail", goodsEntity);
            }
        });

        showToast("搜索成功！");
    }
}